<?php //pack11class

class SearchForm extends Form {

    public $onclick="";
    public $traceEnabled=true;

	public function setup() {
        $this->id="SearchForm";
        $this->name="SearchForm";
        if(array_key_exists("search",$_GET)) $this->extra="1";

        $firstDefined=false;

        $word=new TextInput;
        $word->property_name="word_search";
        $word->label=LabelManager::getLabel("search_word_label");
        if(array_key_exists("word_search", $_POST)) $word->setBody($_POST["word_search"]);
        $this->addChild($word);

        foreach(PropertiesManager::getSearchable() as $property) {
            if($property->hasValue || $property->system=="price"){
                $fromto=$this->generateFromTo($property);
                if($fromto) $this->addChild($fromto);
                if($property->system=="price") {
                    $showPrice=$this->generatePriceCheck();
                    if($showPrice) $this->addChild($showPrice);
                    $select=$this->generateCurrencySelect();
                    if($select) $this->addChild($select);
                }
            } else {
                $select=$this->generateSelect($property);
                if($select) $this->addChild($select);
            }
        }

    }

    private function generateSelect(Property $property) {
        $select=new SelectInput;

        $option=new SelectOption;
        $option->label=LabelManager::getLabel("searchSelectNull");
        $option->value=0;
        if(!$property->value) $option->selected=SelectOption::SELECTED;
        $select->addChild($option);

        $options=$property->availableValues();
        if(count($options)==0) $select->addStyle("display","none");
        foreach($options as $value) {
            $option=new SelectOption;
            $option->label=$value->value;
            $option->value=$value->id;
            if($property->value==$value->id) $option->selected=SelectOption::SELECTED;
            $select->addChild($option);
        }

        $select->label=$property->label;
        $select->property_name=$property->system;
        $select->events=str_replace('\"', '"', "onChange=\"SearchBlock.rebuildSelects(this);\"");
        $select->class="select";
        return $select;
    }

    private function generateCurrencySelect() {
        $select=new SelectInput;
        $currencies=CurrencyManager::getCurrencies();
        if(!$currencies) {
            //trace("currencies error");
            return false;
        }
        foreach($currencies as $currency) {
            $option=new SelectOption;
            $option->label=$currency->label;
            $option->value=$currency->id;
            $option->selected=($currency==CurrencyManager::currentCurrency())? SelectOption::SELECTED:"";
            $select->addChild($option);
        }

        $select->label=LabelManager::getLabel("search_currency_label");
        $select->property_name="currency";
        //$select->events=str_replace('\"', '"', "onChange=\"CurrencyManager.setCurrent(this);\"");
        $select->class="select";
        $select->lamp_class="hidden";
        $select->type="currency";
        return $select;
    }

    private function generateFromTo(Property $property){
        $fromtoinput=new FromToInput;
        $fromtoinput->label=$property->label." ".LabelManager::getLabel("searchFromTo");
        $fromtoinput->property_name=$property->system;
        $fromtoinput->class="fromtoinput";
        $fromtoinput->draw();
        return $fromtoinput;
        //return false;
    }

    private function generatePriceCheck() {
        $item=new CheckBoxInput;
        $item->property_name="showPrice";
        $item->label=LabelManager::getLabel("showPrice_checkbox_label");
        $item->checked=(ObjectsManager::$showPrice)? CheckBoxInput::CHECKED:"";
        $item->formid="SearchForm";
        $item->events=str_replace('\"', '"', "onChange=\"SearchBlock.setShowPrice(this);\"");
        $item->setClass("SearchFormShowPrice");
        return $item;
    }

}
?>
